﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.DAO.Admin;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.BO.Admin
{
    public class UserInRoleBO : BaseBO
    {
        #region Field list in database
        public const string FIELD_EMAIL = "Email";
        public const string FIELD_FULL_NAME = "FullName";
        public const string FIELD_USER_IN_ROLE_ID = "UserInRoleID";
        public const string FIELD_USER_ID = "UserID";
        public const string FIELD_ROLE_ID = "RoleID";
        public const string FIELD_DATE_CREATED = "DateCreated";
        public const string FIELD_DATE_MODIFIED = "DateModified";
        public const string FIELD_IS_ACTIVE = "IsActive";
        public const string FIELD_DESCRIPTION = "Descriptions";
        public const string FIELD_ROLE_NAME = "RoleName";
        public const string FIELD_USER_NAME = "UserName";
        public const string FIELD_USER_CODE = "UserCode";

        #endregion
        public const string FIELD_RECORD_COUNT = "RecordCount";

        private String _connectionString;

        public UserInRoleBO(string connString)
        {
            this._connectionString = connString;

        }
        public UserInRoleBO()
        {
            this._connectionString = Common.ConnectionUtil.DHQG_ADMIN_CONNECTION_STRING;

        }

        public UserInRoleDAO _UserInRoleDAO;

        public UserInRoleDAO CreateSQLDAO()
        {
            return new UserInRoleDAO(_connectionString, DEFAULT_MAPPER_TYPE);
        }

        public DataPage<UserInRoleModel> GetDynamicListPaging(UserInRoleModel aModel, int pageSize, int pageIndex, bool compareEqual)
        {
            try
            {
                _UserInRoleDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {UserInRoleModel.EMAIL,FIELD_EMAIL},
                    {UserInRoleModel.FIELD_FULL_NAME,FIELD_FULL_NAME},
                    {UserInRoleModel.FIELD_USER_IN_ROLE_ID,FIELD_USER_IN_ROLE_ID},
                    {UserInRoleModel.FIELD_USER_ID,FIELD_USER_ID},
                    {UserInRoleModel.FIELD_ROLE_ID,FIELD_ROLE_ID},  
                    {UserInRoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION},  
                    {UserInRoleModel.FIELD_USER_NAME,FIELD_USER_NAME},  
                    {UserInRoleModel.FIELD_ROLE_NAME,FIELD_ROLE_NAME},  
                    {UserInRoleModel.FIELD_USER_CODE,FIELD_USER_CODE},  
                    {UserInRoleModel.FIELD_DATE_CREATED,FIELD_DATE_CREATED},
                    {UserInRoleModel.FIELD_DATE_MODIFIED,FIELD_DATE_MODIFIED},
                    {UserInRoleModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };
                return _UserInRoleDAO.GetDynamicListPaging(aModel, pageSize, pageIndex, mapDictionary, compareEqual);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public UserInRoleModel GetUserInRoleById(int? id)
        {
            try
            {
                _UserInRoleDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                     {UserInRoleModel.EMAIL,FIELD_EMAIL},
                    {UserInRoleModel.FIELD_FULL_NAME,FIELD_FULL_NAME},
                    {UserInRoleModel.FIELD_USER_IN_ROLE_ID,FIELD_USER_IN_ROLE_ID},
                    {UserInRoleModel.FIELD_USER_ID,FIELD_USER_ID},
                    {UserInRoleModel.FIELD_ROLE_ID,FIELD_ROLE_ID},  
                    {UserInRoleModel.FIELD_ROLE_NAME,FIELD_ROLE_NAME},  
                    {UserInRoleModel.FIELD_USER_NAME,FIELD_USER_NAME}, 
                    {UserInRoleModel.FIELD_USER_CODE,FIELD_USER_CODE},  
                     {UserInRoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION}, 
                    {UserInRoleModel.FIELD_DATE_CREATED,FIELD_DATE_CREATED},  
                    {UserInRoleModel.FIELD_DATE_MODIFIED,FIELD_DATE_MODIFIED},
                    {UserInRoleModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };

                return _UserInRoleDAO.GetById(id, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool createOrUpdateUserInRole(UserInRoleModel model, int[] listID)
        {
            try
            {
                _UserInRoleDAO = CreateSQLDAO();
                return _UserInRoleDAO.createOrUpdateUserInRole(model, listID);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public List<UserInRoleModel> GetListUserInRoleByRoleID(int RoleID)
        {
            try
            {
                _UserInRoleDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {UserInRoleModel.EMAIL,FIELD_EMAIL},
                    {UserInRoleModel.FIELD_FULL_NAME,FIELD_FULL_NAME},
                    {UserInRoleModel.FIELD_USER_IN_ROLE_ID,FIELD_USER_IN_ROLE_ID},
                    {UserInRoleModel.FIELD_USER_ID,FIELD_USER_ID},
                    {UserInRoleModel.FIELD_ROLE_ID,FIELD_ROLE_ID},  
                    {UserInRoleModel.FIELD_ROLE_NAME,FIELD_ROLE_NAME},  
                    {UserInRoleModel.FIELD_USER_NAME,FIELD_USER_NAME}, 
                    {UserInRoleModel.FIELD_USER_CODE,FIELD_USER_CODE},  
                    {UserInRoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION}, 
                    {UserInRoleModel.FIELD_DATE_CREATED,FIELD_DATE_CREATED},  
                    {UserInRoleModel.FIELD_DATE_MODIFIED,FIELD_DATE_MODIFIED},
                    {UserInRoleModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };
                return _UserInRoleDAO.GetListUserInRoleByRoleID(RoleID, mapDictionary);
            }
            catch (Exception e)
            {
                throw;
            }
        }
        

        public bool deleteUserInRole(int[] ID)
        {
            try
            {
                _UserInRoleDAO = CreateSQLDAO();
                return _UserInRoleDAO.deleteUserInRole(ID);
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}